package com.xgxf.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgxf.server.common.BaseResult;
import com.xgxf.server.common.JsonResult;
import com.xgxf.server.constant.TestConstant;
import com.xgxf.server.mapper.TestResultMapper;
import com.xgxf.server.pojo.*;
import com.xgxf.server.service.ITestResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestResultServiceImpl implements ITestResultService {

    @Autowired
    private TestResultMapper testResultMapper;

    public JsonResult getList(TestResultQuery parameterQuery) {
        IPage<TestResultOverall> iPage = new Page<>(parameterQuery.getPage(),parameterQuery.getLimit());
        IPage<TestResultOverall> pageData = testResultMapper.getList(iPage,parameterQuery);
        List<TestResultOverall> records = pageData.getRecords();
        for (TestResultOverall result : records) {
            // 去除多余空格
            result.setOP(this.stringTrim(result.getOP()));
            result.setSubjectiveVerification(this.stringTrim(result.getSubjectiveVerification()));
            result.setInitialTesting(this.stringTrim(result.getInitialTesting()));
            result.setEmptyLoadDetection(this.stringTrim(result.getEmptyLoadDetection()));
            result.setFrontalLoadDetection(this.stringTrim(result.getFrontalLoadDetection()));
            result.setOverloadDetection(this.stringTrim(result.getOverloadDetection()));
            result.setClimbingDetection(this.stringTrim(result.getClimbingDetection()));
        }
        pageData.setRecords(records);
        return JsonResult.success(pageData,"列表获取成功！");
    }

    /**
     *  获取某辆车的检测结果详情
     * @param parameterQuery
     * @return
     */
    @Override
    public JsonResult getDetails(TestResultQuery parameterQuery) {
        // 结果
        TestResultVo result = new TestResultVo();
        TestResultOverall temp = testResultMapper.getOverall(parameterQuery);
        result.setType(temp.getType());
        result.setVin(temp.getVin());
        result.setTestTime(temp.getTestTime());
        result.setOP(this.stringTrim(temp.getOP()));
        result.setBenchID(temp.getBenchID());
        result.setControllerName(temp.getControllerName());

        /**
         * 主观检测项合格情况
         */
        List<BaseResult> subjectiveResults = new ArrayList<>();

        // 主观查验项与合格情况
        this.addSubjectiveResultsToVO(1,parameterQuery,subjectiveResults);
        // 初检测项与合格情况
        this.addSubjectiveResultsToVO(2,parameterQuery,subjectiveResults);
        // 空载检测项与合格情况
        this.addSubjectiveResultsToVO(3,parameterQuery,subjectiveResults);
        // 额载检测项与合格情况
        this.addSubjectiveResultsToVO(4,parameterQuery,subjectiveResults);
        // 超载检测项与合格情况
        this.addSubjectiveResultsToVO(5,parameterQuery,subjectiveResults);
        // 行走检测项与合格情况
        this.addSubjectiveResultsToVO(6,parameterQuery,subjectiveResults);

        result.setSubjectiveTestResults(subjectiveResults);

    /**
     * 客观检测项数值与合格情况
     */

        /**
         * 空载检测项数值与合格情况
         */

        // 获取客观检测数据值
        ObjectiveValues objectiveValues = testResultMapper.getObjectiveValues(parameterQuery);

        // 获取检测标准值
        StandardValues standardValues = testResultMapper.getStandardValues(parameterQuery);

        // 初始化
        List<BaseResult> objectiveResults = new ArrayList<BaseResult>();

        // 空载起最小作业高度
        this.addObjectiveResultsToVO(1,objectiveValues.getEmptyupMinWorkH(),
                standardValues.getEmptyupMinWorkHmax(),standardValues.getEmptyupMinWorkHmin(),objectiveResults);

        // 空载起最大作业高度
        this.addObjectiveResultsToVO(2,objectiveValues.getEmptyupMaxWorkH(),
                standardValues.getEmptyupMaxWorkHmax(),standardValues.getEmptyupMaxWorkHmin(),objectiveResults);

        // 空载起最小电机控制电压
        this.addObjectiveResultsToVO(3,objectiveValues.getEmptyupMinMotorV(),
                standardValues.getEmptyupMinMotorVmax(),standardValues.getEmptyupMinMotorVmin(),objectiveResults);

        // 空载起最大电机控制电压
        this.addObjectiveResultsToVO(4,objectiveValues.getEmptyupMaxMotorV(),
                standardValues.getEmptyupMaxMotorVmax(),standardValues.getEmptyupMaxMotorVmin(),objectiveResults);

        // 空载起最小压力
        this.addObjectiveResultsToVO(5,objectiveValues.getEmptyupMinP(),
                standardValues.getEmptyupMinPmax(),standardValues.getEmptyupMinPmin(),objectiveResults);

        // 空载起最大压力
        this.addObjectiveResultsToVO(6,objectiveValues.getEmptyupMaxP(),
                standardValues.getEmptyupMaxPmax(),standardValues.getEmptyupMaxPmin(),objectiveResults);

        // 空载起平台偏摆量
        this.addObjectiveResultsToVO(7,objectiveValues.getEmptyupOffset(),
                standardValues.getEmptyupOffsetmax(),standardValues.getEmptyupOffsetmin(),objectiveResults);

        // 空载起最大振动量
        this.addObjectiveResultsToVO(8,objectiveValues.getEmptyupMaxVibration(),
                standardValues.getEmptyupMaxVibrationmax(),standardValues.getEmptyupMaxVibrationmin(),objectiveResults);

        // 空载起坑洞检测结果
        this.addObjectiveResultsToVO(9,objectiveValues.getEmptyupPitcheck(),
                standardValues.getEmptyupPitcheckmax(),standardValues.getEmptyupPitcheckmin(),objectiveResults);

        // 空载起升时间
        this.addObjectiveResultsToVO(10,objectiveValues.getEmptyupLiftT(),
                standardValues.getEmptyupLiftTmax(),standardValues.getEmptyupLiftTmin(),objectiveResults);

        // 空载起最小电机电流
        this.addObjectiveResultsToVO(11,objectiveValues.getEmptyupMinMotorA(),
                standardValues.getEmptyupMinMotorCurrentMax(),standardValues.getEmptyupMinMotorCurrentMin(),objectiveResults);

        // 空载起最大电机电流
        this.addObjectiveResultsToVO(12,objectiveValues.getEmptyupMaxMotorA(),
                standardValues.getEmptyupMaxMotorCurrentMax(),standardValues.getEmptyupMaxMotorCurrentMin(),objectiveResults);

        // 空载落下降阀最小电压
        this.addObjectiveResultsToVO(13,objectiveValues.getEmptydownMinValveV(),
                standardValues.getEmptydownMinValveVmax(),standardValues.getEmptydownMinValveVmin(),objectiveResults);

        // 空载落下降阀最大电压
        this.addObjectiveResultsToVO(14,objectiveValues.getEmptydownMaxValveV(),
                standardValues.getEmptydownMaxValveVmax(),standardValues.getEmptydownMaxValveVmin(),objectiveResults);

        // 空载落最小压力
        this.addObjectiveResultsToVO(15,objectiveValues.getEmptydownMinP(),
                standardValues.getEmptydownMinPmax(),standardValues.getEmptydownMinPmin(),objectiveResults);

        // 空载落最大压力
        this.addObjectiveResultsToVO(16,objectiveValues.getEmptydownMaxP(),
                standardValues.getEmptydownMaxPmax(),standardValues.getEmptydownMaxPmin(),objectiveResults);

        // 空载落安全高度
        this.addObjectiveResultsToVO(17,objectiveValues.getEmptydownSafeH(),
                standardValues.getEmptydownSafeHmax(),standardValues.getEmptydownSafeHmin(),objectiveResults);

        // 空载落最大振动量
        this.addObjectiveResultsToVO(18,objectiveValues.getEmptydownMaxVibration(),
                standardValues.getEmptydownMaxVibrationmax(),standardValues.getEmptydownMaxVibrationmin(),objectiveResults);

        // 空载落坑洞检测结果
        this.addObjectiveResultsToVO(19,objectiveValues.getEmptydownPitcheck(),
                standardValues.getEmptydownPitcheckmax(),standardValues.getEmptydownPitcheckmin(),objectiveResults);

        // 空载下降时间
        this.addObjectiveResultsToVO(20,objectiveValues.getEmptydownLiftT(),
                standardValues.getEmptydownLiftTmax(),standardValues.getEmptydownLiftTmin(),objectiveResults);

        // 空载行走最大振动量
        this.addObjectiveResultsToVO(21,objectiveValues.getEmptywalkMaxVibration(),
                standardValues.getEmptywalkMaxVibrationmax(),standardValues.getEmptywalkMaxVibrationmin(),objectiveResults);

        /**
         * 额载检测项数值与合格情况
         */

        // 额载最小压力
        this.addObjectiveResultsToVO(22,objectiveValues.getRatedUPMinP(),
                standardValues.getRatedUPMinPmax(),standardValues.getRatedUPMinPmin(),objectiveResults);

        // 额载最大压力
        this.addObjectiveResultsToVO(23,objectiveValues.getRatedUPMaxP(),
                standardValues.getRatedUPMaxPmax(),standardValues.getRatedUPMaxPmin(),objectiveResults);

        // 额载下沉量
        this.addObjectiveResultsToVO(24,objectiveValues.getRatedUPSink(),
                standardValues.getRatedUPSinkmax(),standardValues.getRatedUPSinkmin(),objectiveResults);

        // 额载起升最大振动量
        this.addObjectiveResultsToVO(25,objectiveValues.getRatedUPMaxVibration(),
                standardValues.getRatedUPMaxVibrationmax(),standardValues.getRatedUPMaxVibrationmin(),objectiveResults);

        // 额载起升时间
        this.addObjectiveResultsToVO(26,objectiveValues.getRatedUPLiftT(),
                standardValues.getRatedUPLiftTmax(),standardValues.getRatedUPLiftTmin(),objectiveResults);

        // 额载最小电机电流
        this.addObjectiveResultsToVO(27,objectiveValues.getRatedupMinMotorA(),
                standardValues.getRatedupMinMotorCurrentMax(),standardValues.getRatedupMinMotorCurrentMin(),objectiveResults);

        // 额载最大电机电流
        this.addObjectiveResultsToVO(28,objectiveValues.getRatedupMaxMotorA(),
                standardValues.getRatedupMaxMotorCurrentMax(),standardValues.getRatedupMaxMotorCurrentMin(),objectiveResults);

        // 额载下降时间
        this.addObjectiveResultsToVO(29,objectiveValues.getRateddownLiftT(),
                standardValues.getRateddownLiftTmax(),standardValues.getRateddownLiftTmin(),objectiveResults);

        // 额载下降最大振动量
        this.addObjectiveResultsToVO(30,objectiveValues.getRateddownMaxVibration(),
                standardValues.getRateddownMaxVibrationmax(),standardValues.getRateddownMaxVibrationmin(),objectiveResults);

        // 额载行走最大振动量
        this.addObjectiveResultsToVO(31,objectiveValues.getRatedwalkMaxVibration(),
                standardValues.getRatedwalkMaxVibrationmax(),standardValues.getRatedwalkMaxVibrationmin(),objectiveResults);

        // 额载左转向时间
        this.addObjectiveResultsToVO(32,objectiveValues.getRatedwalkTurnTimeL(),
                standardValues.getRatedwalkTurnTimeLMax(),standardValues.getRatedwalkTurnTimeLMin(),objectiveResults);

        // 额载右转向时间
        this.addObjectiveResultsToVO(33,objectiveValues.getRatedwalkTurnTimeR(),
                standardValues.getRatedwalkTurnTimeRMax(),standardValues.getRatedwalkTurnTimeRMin(),objectiveResults);

        /**
         * 其他客观检测项数值与合格情况
         */

        // 低空低速行驶速度
        this.addObjectiveResultsToVO(34,objectiveValues.getWalkLowLspeed(),
                standardValues.getClimbLowlowspeedmax(),standardValues.getClimbLowlowspeedmin(),objectiveResults);

        // 低空高速行驶速度
        this.addObjectiveResultsToVO(35,objectiveValues.getWalkLowHspeed(),
                standardValues.getClimbLowhighspeedmax(),standardValues.getClimbLowhighspeedmin(),objectiveResults);

        // 低速刹车距离
        this.addObjectiveResultsToVO(36,objectiveValues.getLowbrakingDistance(),
                standardValues.getLowbrakingDistanceMax(),standardValues.getLowbrakingDistanceMin(),objectiveResults);

        // 高速刹车距离
        this.addObjectiveResultsToVO(37,objectiveValues.getHighbrakingDistance(),
                standardValues.getHighbrakingDistanceMax(),standardValues.getHighbrakingDistanceMin(),objectiveResults);

        // 高空低速行驶速度
        this.addObjectiveResultsToVO(38,objectiveValues.getWalkHighLspeed(),
                standardValues.getClimbHighlowspeedmax(),standardValues.getClimbHighlowspeedmin(),objectiveResults);

        // 高空低速刹车距离
        this.addObjectiveResultsToVO(39,objectiveValues.getHighlowbrakingDistance(),
                standardValues.getHighlowbrakingDistanceMax(),standardValues.getHighlowbrakingDistanceMin(),objectiveResults);

        result.setObjectiveTestResults(objectiveResults);
        return JsonResult.success(result,"检测结果获取成功！");
    }

    // 字符串去空格
    public String stringTrim(String input){
        if (input != null && input.length() != 0){
            return input.trim();
        };
        return input;
    }

    // 主观检测项合格情况（二选一：只返回主观检测项次id，需要从static map里面获取检测项名称）
    public void addSubjectiveResultsToVO(Integer testConstantKey, TestResultQuery testResultQuery,List<BaseResult> subjectiveResults){

        List<BaseResult> baseResultList = testResultMapper.obtainSubjectiveResults(TestConstant.numMap.get(testConstantKey),
                TestConstant.nameMap.get(testConstantKey), testResultQuery);
        for (BaseResult br : baseResultList) {
            // 去除sr.test_id的多余空格
            br.setItem(this.stringTrim(br.getItem()));
            // 将test_id转为实际检测项名称
            br.setItem(TestConstant.subjectiveCategoryMap.get(testConstantKey).get(Integer.valueOf(br.getItem())));
            // 去除subjective_results.result的多余空格
            br.setResult(this.stringTrim(br.getResult()));
            subjectiveResults.add(br);
        }
    }


    // 客观检测项合格情况
    public void addObjectiveResultsToVO(Integer objectiveNameKey, double ObjectiveValue,
                                        double maxValue, double minValue, List<BaseResult> objectiveResults){
        // 判断浮点数类型检测结果是否为null，否则保留两位小数
        if (Double.valueOf(ObjectiveValue) != null){
            // 初始化列表元素
            BaseResult baseResult = new BaseResult();
            // 检测值赋值
            double testValue = Double.valueOf(String.format("%.2f",ObjectiveValue));
            baseResult.setValue(testValue);
            // 检测项名称赋值
            baseResult.setItem(TestConstant.objectiveNameMap.get(objectiveNameKey));

            if (testValue <= maxValue && testValue >= minValue){
                baseResult.setResult("合格");
            } else {
                baseResult.setResult("不合格");
            }
            objectiveResults.add(baseResult);
        }
    }

    @Override
    public JsonResult getTypeList() {
        List<String> data = testResultMapper.getTypeList();
        return JsonResult.success(data,"列表获取成功！");
    }

    @Override
    public JsonResult getVinList() {
        List<String> data = testResultMapper.getVinList();
        return JsonResult.success(data,"列表获取成功！");
    }


}